// logs.js
// const util = require('../../utils/util.js')
const client=require('../../utils/client.js')
Page({
  data: {
    name:'',
    phone:'',
    shortPhone:'',
    unit:'',
    xzList:[]
  },
  onLoad() {
    let _that=this;
    let n=[];
    client.request('/app/wx/xz','get',{},(d)=>{
      console.log(d.data);
      d.data.forEach((e)=>{
        n.push(e.display);        
      })
      _that.setData({
        xzList:n
      })
    })
  },
  onShow(){
    client.request('/app/wx/phone','get',{},(d)=>{
      this.setData({phone:d.data})
    },(ee)=>{

    })

  },
  bindXZChange: function(e) {
    console.log('e.detail.value',e.detail.value)
    this.setData({
      unit: this.data.xzList[e.detail.value]
    })
  },
  bindNameInput: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  /**
   * 表单提交
   */
  saveData: function(e) {
    let _this = this;
    var values = e.detail.value;
    // 表单验证
    if (!_this.validation(values)) {
      wx.showModal({
        content: _this.data.error,
        showCancel: false
      });
      return false;
    }
    client.request('/app/wx/apply','post',values,(e)=>{
     if(e.code=='0'){
      wx.showToast({
        title: '保存成功',
        success:()=>{
          wx.navigateBack();
            // wx.redirectTo({
            //   url: '/pages/home/home',
            // })
        }
      });
     }
    })
  },

  /**
   * 表单验证
   */
  validation: function(values) {
    if (values.name === '') {
      this.data.error = '姓名不能为空';
      return false;
    }
    if (values.unit.length < 1) {
      this.data.error = '所在单位不能为空';
      return false;
    }
    return true;
  },
})
